package com.caiden_micheal.base.utils;

import com.caiden_micheal.base.constant.LoggerHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class EncodeURIComponent {
    private static final Logger logger = LoggerFactory.getLogger(EncodeURIComponent.class);

    public static String encode(String originalString, String enc) {
        try {
            return URLEncoder.encode(originalString, enc)
                    .replace("+", "%20")
                    .replace("!", "%21")
                    .replace("'", "%27")
                    .replace("(", "%28")
                    .replace(")", "%29")
                    .replace("*", "%2A");
        } catch (UnsupportedEncodingException e) {
            logger.error(LoggerHead.errorHead() + "URL编码异常：" + originalString + ", " + e.getMessage());
        }
        return null;
    }

    public static String decode(String originalString, String enc) {
        try {
            return URLDecoder.decode(originalString, enc);
        } catch (UnsupportedEncodingException e) {
            logger.error(LoggerHead.errorHead() + "URL解码异常：" + originalString + ", " + e.getMessage());
        }
        return null;
    }
}